<?php
class Member_positionAction extends IntesysAction {
	
	function _initialize(){
        parent::_initialize();
        $this->dao = D('Member_position');// 编辑、删除操作需要
    }
	public function index(){
		$this->display();
	}
	public function indexiframe(){
		$list=$this->dao->getListToArray1(0,__SITEID__);
		$this->assign('list',$list);
		$this->display();
	}
	public function delete(){
		addDetection('Position');
		
		if($this->_get('id')){
			//if($this->dao -> where(array('pid' => $this->_get('id'))) -> find()){
				
			if($this->dao -> where(array('pid' => $this->_get('id'))) -> find()){
				$this->error('请先删除子部门');exit;
			}
			if(M('Member') -> where(' memberpositionid like "%'.$this->_get('id').'%" and site_id ='.__SITEID__) -> find()){
				$this->error('该部门下还有员工');exit;
			}
			parent::delete();
		}
	
	
	}
	public function update(){
		addDetection('Position');
		C('TOKEN_ON', false);
		if(IS_POST){
			$memberDB=M('Member');
			$_POST['name']=str_replace(array("\r\n","\n","\r"),'',$_POST['name']);
			if($this->_post('member_id')){
				$memberDB->where(['memberpositionid'=>['like',','.$_POST['id'].',']])->setField('isPositionAdmin',0);
				$memberDB->where(['id'=>$this->_post('member_id')])->setField('isPositionAdmin',1);	
			}
			
			parent::update();
			echo $this->dao -> getLastSql();
				
		}
	
	}
	//更新职位的会员ID
	public function updateMember(){
		if(IS_POST){
			$member=D('Member')->where('id='.$_POST['id'])->find();	
			$_POST['member_id']=$member['id'];
			$_POST['member_name']=$member['name'];
			$_POST['id']=explode(',',$member['memberpositionid'][1]);
			$this->dao->save($_POST);
			die(json_encode(['status'=>1,info=>'操作成功']));
		}
	
	}
	public function change(){
		if($_GET['id']){
			$this->list=$this->dao->where('id='.$_GET['id'])->find();
			$this->display();	
		}
	}
	public function tree(){
		$map['pid'] = 0;
		if($this->_get('pid')){
			$map['pid'] = $this->_get('pid');
		}
		$map['site_id'] = __SITEID__;
		$list = $this->dao -> where($map) -> Field('name,id') -> select();
		foreach($list as $k => $v){
			//查找有无下级
			$sub = $this-> dao -> where(array('site_id' => __SITEID__, 'pid' => $v['id'])) -> find();
			$list[$k]['type'] = $sub ? 'folder' : 'item';
			$list[$k]['additionalParameters'] = array('id' => $v['id']);
		}
		echo json_encode($list);
	}
	
	
	
	public function insert(){
		C('TOKEN_ON', false);
		if(IS_POST){ 
			$_POST['site_id']=__SITEID__;
			$_POST['status']=1;
			$_POST['name']=str_replace(array("\r\n","\n","\r"),'',$_POST['name']);
			if ($this->dao->create()) {
            $result = $this->dao->add();
            if (false !== $result) {
				
					$ret['status'] = 1;
					$ret['info'] = '增加成功';
					$ret['id'] = $result;
					echo json_encode($ret);
               
            } else {

                $this->error($this->dao->getError());
            }
        } else {

            $this->error($this->dao->getError());
        }
		}
			addDetection('Position');
	}
	//获取当前站点部门
	public function getCount(){
		$count=$this->dao->where(['site_id'=>__SITEID__])->count();
		die(json_encode(['status'=>1,'tbody'=>$count]));		
	}
}

